<template>
  <div class="main_box">
    <el-row>
      <el-col :span="12">
        <Crumbs></Crumbs>
      </el-col>
      <el-col :span="12">
        <div class="opration_wrap"></div>
      </el-col>
    </el-row>
    <el-row>
      <div class="search_wrap">
        <el-form :inline="true" :model="searchForm" ref="searchForm">
          <el-form-item prop="pageName">
            <el-input v-model="searchForm.pageName" :clearable="true" placeholder="申请页面名称"></el-input>
          </el-form-item>
          <el-form-item prop="pageCode">
            <el-input v-model="searchForm.pageCode" :clearable="true" placeholder="申请页面编码"></el-input>
          </el-form-item>
          <el-form-item prop="approveUserId">
            <el-select v-model="searchForm.approveUserId" :clearable="true" placeholder="请选择申请人" filterable>
              <el-option v-for="item in listAdmin" :key="item.userId" :label="item.userName" :value="item.userId"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="examineUserId">
            <el-select v-model="searchForm.examineUserId" :clearable="true" placeholder="请选择审核人" filterable>
              <el-option v-for="item in listAdmin" :key="item.userId" :label="item.userName" :value="item.userId"></el-option>
            </el-select>
          </el-form-item>

          <el-form-item prop="state">
            <el-select v-model="searchForm.state" :clearable="true" placeholder="状态">
              <el-option key="-1" label="拒绝" value="-1"></el-option>
              <el-option key="0" label="申请中" value="0"></el-option>
              <el-option key="1" label="通过" value="1"></el-option>
              <el-option key="2" label="已导出" value="2"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="onSearch">查询</el-button>
            <el-button @click="resetForm">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-row>
    <el-row ref="tableRow">
      <el-table :data="tableList" :fit="true">
        <el-table-column type="index" label="序号" width="50"></el-table-column>
        <el-table-column prop="pageName" label="申请页面名称" width="110" show-overflow-tooltip></el-table-column>
        <el-table-column prop="pageCode" label="申请页面编码" width="150" show-overflow-tooltip></el-table-column>
        <el-table-column prop="approveUserName" label="申请人姓名" align="center"></el-table-column>
        <el-table-column prop="examineUserName" label="审核人姓名" align="center"></el-table-column>
        <el-table-column prop="approveRemark" label="申请说明" width="200" show-overflow-tooltip></el-table-column>
        <el-table-column prop="state" label="状态" :formatter="formatState" align="center"></el-table-column>
        <el-table-column prop="approveDate" label="申请时间" width="150" align="center"></el-table-column>
        <el-table-column prop="examineDate" label="审核时间" width="150" align="center"></el-table-column>
        <el-table-column prop="exportDate" label="导出时间" width="150" align="center"></el-table-column>
        <el-table-column prop="createDate" label="创建时间" width="150" align="center"></el-table-column>
        <el-table-column label="操作" width="136" fixed="right">
          <template slot-scope="scope">
            <el-row>
              <el-button v-has="permissionCheck" @click="handleClickDetail(scope.row)" size="mini">查看</el-button>
              <el-button v-has="permissionApprove" @click.native="handleClickApprove(scope.row)" size="mini" v-if="scope.row.state == 0">审核</el-button>
            </el-row>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.currentPage"
        :page-sizes="[5, 10, 20, 30]"
        :page-size="page.pageSize"
        layout="total, sizes, jumper, next, pager, prev"
        :total="page.pageTotal"
      ></el-pagination>
    </el-row>

    <el-dialog title="审核申请导出信息" :visible.sync="dialogDataExportApproveVisible" :center="isCenter" width="600px">
      <el-form :model="form" :rules="roleRules">
        <el-form-item label="申请人姓名：" label-width="120px">
          <span>{{ form.approveUserName }}</span>
        </el-form-item>
        <el-form-item label="申请页面名称：" label-width="120px">
          <span>{{ form.pageName }}</span>
        </el-form-item>
        <el-form-item label="申请说明：" label-width="120px">
          <span>{{ form.approveRemark }}</span>
        </el-form-item>
        <el-form-item label="审核结果：" label-width="120px">
          <template>
            <el-radio v-model="form.state" label="1">通过</el-radio>
            <el-radio v-model="form.state" label="-1">拒绝</el-radio>
          </template>
        </el-form-item>
        <el-form-item label="审核说明：" label-width="120px">
          <el-input type="textarea" :clearable="true" v-model="form.examineRemark"></el-input>
        </el-form-item>
        <el-form-item align="center">
          <el-button @click.native="dismissDateApprove">取 消</el-button>
          <el-button type="primary" @click.native="commitDateApprove">确 定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import Crumbs from '@/components/Crumbs';
import * as message from '@/constants/message';
import { FORMAT_DATE_YYYY_MM_DD } from '@/constants/constants';
export default {
  data() {
    return {
      searchForm: {
        pageName: '',
        pageCode: '',
        approveUserId: '',
        examineUserId: '',
        state: '0',
      },
      listAdmin: [],
      tableList: [],

      page: {
        pageNo: 1,
        pageSize: 20,
        pageTotal: 0,
        currentPage: 1,
      },
      permissionCheck: 'SYSTEM_MANAGER_DATA_EXPORT_LIST_SEE',
      permissionApprove: 'SYSTEM_MANAGER_DATA_EXPORT_LIST_APPROVE',

      dialogDataExportApproveVisible: false,
      formLabelWidth: '150px',
      form: {
        deaId: '',
        pageName: '',
        pageCode: '',
        approveUserName: '',
        approveUserRemark: '',
        state: '1',
        examineRemark: '',
      },
      isCenter: true,
      roleRules: {
        state: [{ required: true, message: '请选择审核结果', trigger: 'change' }],
        examineRemark: [{ max: 201, message: '长度不超过200个字', trigger: 'blur' }],
      },
    };
  },
  components: {
    Crumbs,
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      this.listTable();
      this.getTableHeight();
      this.getAdminData();
    },

    formatState(row, column, cellValue) {
      if (cellValue == 0) {
        return '申请中';
      } else if (cellValue == 1) {
        return '通过';
      } else if (cellValue == 2) {
        return '已导出';
      } else if (cellValue == -1) {
        return '拒绝';
      }
    },
    formatDate(value) {
      return this.$Utils.formatDate(value, 'yyyy-MM-dd');
    },
    getAdminData() {
      this.$Get(this.$Config.api.listAdmin)
        .then((res) => {
          this.listAdmin = res.data;
        })
        .catch((error) => {
          console.log(error);
        });
    },
    handleCurrentChange(val) {
      this.page.pageNo = val;
      this.listTable();
    },

    handleClickDetail(row) {
      this.$router.push({ path: '/data/dataExportApproveDetail', query: { id: row.deaId } });
    },
    handleSizeChange(size) {
      this.page.pageSize = size;
      this.listTable();
    },

    handleCurrentChange(val) {
      this.page.pageNo = val;
      this.listTable();
    },
    onSearch() {
      this.page.pageNo = 1;
      this.listTable();
    },
    resetForm() {
      this.$refs.searchForm.resetFields();
      this.onSearch();
    },
    listTable() {
      let data = {
        pageNo: this.page.pageNo,
        pageSize: this.page.pageSize,
        pageName: this.searchForm.pageName,
        pageCode: this.searchForm.pageCode,
        approveUserId: this.searchForm.approveUserId,
        examineUserId: this.searchForm.examineUserId,
        state: this.searchForm.state,
      };
      this.fetchData(data);
    },
    fetchData(data) {
      this.$emit('show-loading', true);
      this.$Get(this.$Config.api.dataExportList, data)
        .then((res) => {
          this.tableList = res.data.list;
          this.page.pageTotal = res.data.total;
          this.page.currentPage = res.data.pageNum;
          this.$emit('show-loading', false);
        })
        .catch((error) => {
          console.error(error);
          this.$emit('show-loading', false);
        });
    },
    handleClickApprove(row) {
      this.form.deaId = row.deaId;
      this.form.pageName = row.pageName;
      this.form.pageCode = row.pageCode;
      this.form.approveUserName = row.approveUserName;
      this.form.approveRemark = row.approveRemark;
      this.form.state = '';
      this.form.examineRemark = '';
      this.dialogDataExportApproveVisible = true;
    },
    dismissDateApprove() {
      this.dialogDataExportApproveVisible = false;
      this.form.state = '';
      this.form.examineRemark = '';
    },
    commitDateApprove() {
      let approveData = {
        deaId: this.form.deaId,
        state: this.form.state,
        examineRemark: this.form.examineRemark,
      };
      let _this = this;
      this.$Put(this.$Config.api.dataExport, approveData)
        .then((res) => {
          _this.dialogDataExportApproveVisible = false;
          this.listTable();
          this.$message({
            type: 'success',
            message: '操作成功',
          });
        })
        .catch((error) => {
          if (error.msg) {
            this.$notify.error({
              title: '错误',
              message: error.msg,
            });
          }
        });
    },
  },
  beforeRouteEnter(to, from, next) {
    next((vm) => {});
  },
};
</script>
<style lang="scss" src="@/assets/css/views/content.scss" scope></style>
